/*
 * jQuery code used on page to manage colors.
 */

$(document).ready(function() {
  /* Gives a default value to the field to create a new color
     when the user clicks on it */
  $('#new_value').focus(function(){
          if (this.value == '') {
              this.value='#FFFFFF'}
      }     
  );

  /* Create a fake input field, so if the user plays with the selector
   * before clicking on a field, no change will be visible. */
  $('body').append('<input type="hidden" class="djips_color" />');
  
  /* Creates the color picker. */
  var f = $.farbtastic('#picker');
  
  /* Link farbastic to color fields. */
  var selected;
  $('.djips_color')
      .each(function () {
         f.linkTo(this);
         f.setColor('#' + this.value); })
      .focus(function() {
          f.linkTo(this);
        });

  /* Hide the # in front of color fields as Farbtastic adds
     one in the field.*/
  $('.color_form_dash').hide()

  /*  */
  $('.delete_color_link').click(function(){
    tmp = this.id.split('_');
    url = admin_base_url + '/djips_common/ajax/delete_color_msg/' +
        tmp[tmp.length - 1] + '/';
    return show_overlay_msg(url, true);
  });

  $('#confirm_delete_image').live('click', function(){
      var msg_loaded = false;
      color_id = $('#deleted_color_id').attr('value');
      url = admin_base_url + '/djips_common/ajax/delete_color/' +
        color_id + '/';

      msg = $.ajax({url: url,
                    async: false,
                    success: function(){msg_loaded = true}}).responseText;

      if (!msg_loaded) {
          return true;          
      }

      fade_and_remove('#tr_color_' + color_id);      
      
      add_status_message(msg, 'info_msg');
      close_overlay_message();
      return false;
  });
});
